package com.software.levelupandroid.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.software.levelupandroid.entity.User;

public class UserUtil {
    private static SharedPreferences sharedPreferences;
    private static final String USER_KEY = "user";
    public static void saveUser(User user, Context context){
        sharedPreferences = context.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = sharedPreferences.edit();
        String userJson = new Gson().toJson(user);
        editor.putString(USER_KEY, userJson);

        Log.i("UserUtils", "user: "+user);

        editor.apply();
    }

    public static User getUser(Context context){
        sharedPreferences = context.getSharedPreferences(USER_KEY, Context.MODE_PRIVATE);

        String userJson = sharedPreferences.getString(USER_KEY, "");

        User user = new Gson().fromJson(userJson, User.class);

        Log.i("UserUtils", "user: "+user);

        return user;
    }
}
